package com.sina.shop.manage.controller.admin;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.admin.Admin;
import com.sina.shop.domain.admin.Role;
import com.sina.shop.manage.service.admin.AdminManageService;
import com.sina.shop.util.StringUtils;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-12-20-16:18
 */
@Controller
@RequestMapping("adminManage")
public class AdminManageController extends BaseController {

    @Qualifier("admin1ManageService")
    @Autowired
    private AdminManageService adminManageService;

    //@RequiresRoles("admin")
    @RequestMapping(value = "getAdminList", method = RequestMethod.GET)
    public ModelAndView getAdminList(@Valid Admin admin) {
        LOG.info("admin:{}", admin);

        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("resultView");
        Long count[] = new Long[1];
        List<Map<String, Object>> adminList = adminManageService.getAdminList(admin, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", adminList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);


        return mv;
    }

    @RequestMapping(value = "getAdminIdAndNameList", method = RequestMethod.GET)
    public ModelAndView getAdminIdAndNameList() {

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);

        List<Map<String, Object>> adminList = adminManageService.getAdminIdAndNameList();

        Map<String, Object> data = new HashMap<>(4);
        data.put("data", adminList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    //@RequiresPermissions("select")
    @RequestMapping(value = "getAdmin", method = RequestMethod.GET)
    public ModelAndView getAdmin(String account) {
        LOG.info("account:{}", account);

        ModelAndView mv = new ModelAndView("resultView");
        Admin admin = adminManageService.getAdmin(account);
        mv.addObject(RESULT, admin);

        return mv;
    }

    @RequestMapping(value = "getAdminById", method = RequestMethod.GET)
    public ModelAndView getAdminById(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Admin admin = adminManageService.getAdmin(id);
        mv.addObject(RESULT, admin);

        return mv;
    }


    //支持批量删除，各个id之间用“，”隔开
    //@RequiresRoles("admin")
    @RequestMapping(value = "deleteAdmin", method = RequestMethod.POST)
    public ModelAndView deleteAdmin(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            adminManageService.deleteAdmin(id);
            result.put(CODE, OK);
            result.put(MSG, "删除用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    //@RequiresRoles("admin")
    @RequestMapping(value = "updateAdmin", method = RequestMethod.POST)
    public ModelAndView updateAdmin(@Valid Admin admin, String rid) {
        LOG.info("admin:{}", admin);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        admin.setRoles(getRById(rid));
        try {
            adminManageService.updateAdmin(admin);
            result.put(CODE, OK);
            result.put(MSG, "更新用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "saveAdmin", method = RequestMethod.POST)
    public ModelAndView saveAdmin(@Valid Admin admin) {
        LOG.info("admin:{}", admin);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            adminManageService.saveAdmin(admin);
            result.put(CODE, OK);
            result.put(MSG, "新增用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "getAdminListToHtml", method = RequestMethod.GET)
    public ModelAndView getAdminListToHtml() {

        ModelAndView mv = new ModelAndView("admin/adminList");
        return mv;
    }

    private Set<Role> getRById(String rid) {

        Set<Role> roles = new HashSet<>();

        if (!StringUtils.isEmpty(rid)) {
            //分割
            String rids[] = rid.split(",");
            for (String r : rids) {
                if (!StringUtils.isEmpty(r)) {
                    //得到对应的permission
                    Role role = new Role();
                    role.setId(Long.valueOf(r));
                    //放入集合
                    roles.add(role);
                }
            }
        } else {
            //如果字符串为空返回null
            roles = null;
        }
        //判断是否为空或者size为0
        if (roles.isEmpty()) {
            return null;
        }
        return roles;
    }
}
